﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace CPPEI.Coolzon.UI.StaticPartLib
{
    /// <summary>
    /// Interaction logic for PartSwitchImage.xaml
    /// </summary>
    [System.ComponentModel.DisplayName("切换显示图片组件")]
    [XPartIcon("pack://application:,,,/CPPEI.Coolzon.UI.StaticPartLib;component/Images/PartIcons/切换图片显示组件.png")]
    [XPart("SwitchImage", "切换显示图片组件")]
    public partial class PartSwitchImage : ControlPart
    {
        [Category("Appearance")]
        [DisplayName("显示的图片")]
        [Description("Images")]
        [Editor(typeof(UI.XProperty.Editors.XImageSourceFileArrayEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Appearance, "Images", "显示的图片", false)]
        public ImageSourceFile[] XImages
        {
            get
            {
                return _XImages;
            }
            set
            {
                _XImages = value;
                Refresh();
            }
        }

        [Category("Appearance")]
        [DisplayName("切换时间间隔")]
        [Description("SwitchInterval")]
        [Editor(typeof(UI.XProperty.Editors.XDoubleEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Appearance, "SwitchInterval", "切换时间间隔", false)]
        public double XSwitchInterval
        {
            get
            {
                return _XSwitchInterval;
            }
            set
            {
                _XSwitchInterval = value;
                if (_switchTimer != null)
                {
                    _switchTimer.Interval = TimeSpan.FromSeconds(_XSwitchInterval);
                }
                Refresh();
            }
        }

        private Storyboard _loopAnimation;
        private int _imageIndex;
        private DispatcherTimer _switchTimer;
        private ImageSourceFile[] _XImages;
        private double _XSwitchInterval;

        public PartSwitchImage()
        {
            InitializeComponent();

            XSwitchInterval = 0.5d;
            _imageIndex = 0;

            ClipToBounds = true;
        }
        protected override void OnApplyXProperty()
        {
            base.OnApplyXProperty();
            if(_ctlImage== null)
            {
                return;
            }
            if (this.XWidth > (XBorderThickness.Left + XBorderThickness.Right + XPadding.Left + XPadding.Right))
            {
                _ctlImage.Width = this.XWidth - (XBorderThickness.Left + XBorderThickness.Right + XPadding.Left + XPadding.Right);
            }
            else
            {
                _ctlImage.Width = 0;
            }
            if (this.XHeight > (XBorderThickness.Top + XBorderThickness.Bottom + XPadding.Top + XPadding.Bottom))
            {
                _ctlImage.Height = this.XHeight - (XBorderThickness.Top + XBorderThickness.Bottom + XPadding.Top + XPadding.Bottom);
            }
            else
            {
                _ctlImage.Height = 0;
            }
        }
        protected override void OnActive(PartContext context)
        {
            base.OnActive(context);

            if (_switchTimer == null)
            {
                _switchTimer = new DispatcherTimer();
                _switchTimer.Interval = TimeSpan.FromSeconds(this.XSwitchInterval);
                _switchTimer.Tick += _switchTimer_Tick;
                _switchTimer.Start();
            }
            else
            {
                _switchTimer.Start();
            }
        }

        void _switchTimer_Tick(object sender, EventArgs e)
        {
            if (this.XImages != null && this.XImages.Length > 0)
            {
                if (_imageIndex >= this.XImages.Length)
                {
                    _imageIndex = 0;
                }
                this._ctlImage.Source = this.XImages[_imageIndex].ImageSource;
                _imageIndex++;
            }
        }

        protected override void OnDeactive(PartContext context)
        {
            if (_switchTimer != null)
            {
                _switchTimer.Stop();
                _switchTimer = null;
            }

            base.OnDeactive(context);
        }

        protected override void OnDestory()
        {
            if (_switchTimer != null)
            {
                _switchTimer.Stop();
                _switchTimer = null;
            }

            base.OnDestory();
        }
    }
}
